﻿@{
    ViewBag.Title = "Settings";
    Layout = "~/views/shared/_layout.cshtml";
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Settings</title>
    <link href="~/content/web/Settings.css" rel="stylesheet" />
</head>
<body>
    @* Layout-header *@

    <div class="container">
        <div id="filter" class="style-bg-green" style="width: 100%">
            <table class="filter-table">
                <tr>
                    <td>
                        <span id="showOperator" class="showType"><input type="radio" name="showType">Operator</span>
                    </td>
                    <td>
                        <div class="apart">|</div>
                    </td>
                    <td>
                        <span id="showProject" class="showType"><input type="radio" name="showType">Project</span>
                    </td>
                    @*<td>
                        <div class="apart">|</div>
                    </td>
                    <td>
                        <span id="showClient" class="showType"><input type="radio" name="showType">Client</span>
                    </td>*@
                    <td class="tdSearch" >
                        <label for="">Search</label> <i id="search" class="fa fa-search style-green"></i>
<input type="text" class="searchInput" />@*<button id="search"> search</button>*@
                    </td>
                </tr>
            </table>
        </div>


        <table class="data-table">
            <thead>
                <tr id="tb-head"></tr>
            </thead>
            <tbody id="details"></tbody>

            <tfoot>
                <tr>
                    <td id="addFooter"></td>
                </tr>
            </tfoot>


        </table>
    </div>

    <!--弹出层-->
    <div class="output">
        <i id="closeOutput" class="fa fa-remove"></i> 
        <div>
            <table id="outputInfo"></table>
        </div>
    </div>
    <!--遮罩层-->
    <div class="hide"></div>

    <script>

        $(function () {
            /*添加header标记*/
            var flag = "<i class='fa fa-angle-down style-green'></i>"
            $("#Settings").append(flag);

            page.$Init();
        });

        var clientInfo;

        //根据搜索框ID加载对应数据
        function searchData(){
            var seachName = $(".searchInput").attr("id");
            switch (seachName) {
                case "searchOperator":
                    page.$loadOperator();
                    break;
                case "searchProject":
                    page.$loadProject();
                    break;
                case "searchClient":
                    page.$loadClient();
                    break;
            }
        };

        var page = {
            $Init: function () {
                //判断需要显示的数据
                page.$showType();

                /*默认显示Opeartor*/
                $("#showOperator").find("input").attr("checked", "true");
                $(".searchInput").attr({ "id": "searchOperator", "placeholder": "Please Enter User Name" });
                page.$loadOperator();       //获取operator的信息

                //键入Enter或点击图标查询
                page.$searchType();
            },


            //显示弹出层
            $showOutput: function () {
                var h = $(document).height();       //获取内容高度
                $(".hide").css({ display: "block", height: h + "px" });
                $(".output").css("display", "block");
                $("#closeOutput").click(function () {
                    page.$closeOutput();
                });
            },

            //关闭弹出层
            $closeOutput: function () {
                $(".hide").css("display", "none");
                $(".output").css("display", "none");
                $("#outputInfo").empty();
                $(".saveInfo").remove();
            },

            //判断需要显示的数据
            $showType: function () {
                $(".showType").click(function () {
                    $(".searchInput").val(null);        //清空查询条件
                    var showType = $(this).attr("id");
                    $(this).find("input").attr("checked", "true");
                    switch (showType) {
                        case "showOperator":
                            $(".searchInput").attr({ "id": "searchOperator", "placeholder": "Please Enter User Name" });
                            page.$loadOperator();       //重新加载operator的信息
                            break;
                        case "showProject":
                            $(".searchInput").attr({ "id": "searchProject", "placeholder": "Please Enter Project Name" });
                            page.$loadProject();        //重新加载project的信息
                            break;
                        case "showClient":
                            $(".searchInput").attr({ "id": "searchClient", "placeholder": "Please Enter Client Name" });
                            page.$loadClient();     //重新加载client的信息
                            break;
                    }
                })
            },

            //键入Enter或点击图标查询
            $searchType: function () {
                //按下Enter键查询
                $(".tdSearch").keypress(function (e) {
                    var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
                    if (keyCode == 13) {
                        searchData();
                    }
                });
                //点击搜索符号查询
                $("#search").click(function () {
                    searchData();
                });
            },

            //获取operator的信息
            $loadOperator: function () {
                var str = "";
                var operatorHead = "<th>User Name</th><th>Project</th><th></th>";
                $.ajax({
                    url: "/api/Settings/OperatorInfo",
                    data: { 'MemberName': $("#searchOperator").val() },
                    type: "post",
                    success: function (info) {
                        $.each(info, function (j, OperatorItem) {
                            var project = OperatorItem.Project;
                            str += "<tr><td id='OperatorUserName" + OperatorItem.OperatorID + "'> "+ OperatorItem.UserName + "</td><td id='OperatorProjectName" + OperatorItem.OperatorID + "'></td><td><a href='javascript:ResetPassword(" + OperatorItem.OperatorID + ");'>Reset</a><a href=\"javascript:operation.$editOperator(" + OperatorItem.ProjectID + "," + OperatorItem.OperatorID + "," + "'" + OperatorItem.Project + "'" + ");\">Edit</a><a  href=\"javascript:DisableOperator(" + OperatorItem.OperatorID + ");\">Disable</a></td></tr>";
                            page.$loadOperatorProject(OperatorItem.OperatorID);
                        });
                        $("#details").html(str);
                        $("#tb-head").html(operatorHead);
                        $("#addFooter").html("<a id='addOperator' href='javascript:operation.$addOperator()'><i class='fa fa-plus'></i> Add Operator</a>");
                    }

                });
            },
            //获取operator的project信息
            $loadOperatorProject: function (OperatorID) {
                var OperatorProjectVal = "", maxWidth = 20;
                $.ajax({
                    url: "/api/Settings/ApplicationInfoByOperator",
                    data: { 'MemberID': OperatorID },
                    dataType: "json",
                    type: "post",
                    success: function (info) {
                        $.each(info, function (j, item) {
                            OperatorProjectVal += $.trim(item.Project) + " ";
                        });
                        /*长度超出部分省略*/
                        OperatorProjectVal = OperatorProjectVal.length > maxWidth ? OperatorProjectVal.substring(0, maxWidth) + "..." : OperatorProjectVal;
                        $("#OperatorProjectName" + OperatorID).append(OperatorProjectVal);
                    }
                });
            },
            //获取project的信息
            $loadProject: function () {
                var projectStr = "";
                var projectHead = "<th>Project</th><th>Client</th><th>Issue Type</th><th></th>";
                $.ajax({
                    url: "/api/Settings/GetApplicationInfo",
                    data: { 'ProjectName': $("#searchProject").val() },
                    success: function (info) {
                        $.each(info, function (j, ProjectItem) {
                            ;
                            projectStr += "<tr><td id='ProjectName" + ProjectItem.ID + "'>" + ProjectItem.Name + "</td><td id='ClientName" + ProjectItem.ID + "'>" + ProjectItem.Client.Name + "</td><td><span id='omitIssueType" + ProjectItem.ID + "'></span></td><td><a href='javascript:operation.$editProject(" + ProjectItem.ID + "," + ProjectItem.IsWeChatActive + ");'>Edit</a><a  href=\"javascript:DisableProject(" + ProjectItem.ID + ");\">Disable</a></td></tr>";
                            page.$loadProjectIssueType(ProjectItem.ID);       //写入Issue Type
                        });
                        $("#details").html(projectStr);
                        $("#tb-head").html(projectHead);
                        $("#addFooter").html("<a id='addProject' href='javascript:operation.$addProject()'><i class='fa fa-plus'></i> Add Project</a>");
                    }
                });
            },


            //获取project的IssueType信息
            $loadProjectIssueType: function (ProjectID) {
                var IssueTypeVal = "", maxWidth = 20;
                $.ajax({
                    url: "/api/Settings/GetIssueTypeInfos",
                    data: { 'ProjectID': ProjectID },
                    dataType: "json",
                    success: function (info) {
                        $.each(info, function (j, item) {
                            IssueTypeVal += $.trim(item.IssueTypeDescription) + " ";
                        });
                        /*长度超出部分省略*/
                        IssueTypeVal = IssueTypeVal.length > maxWidth ? IssueTypeVal.substring(0, maxWidth) + "..." : IssueTypeVal;
                        $("#omitIssueType" + ProjectID).append(IssueTypeVal);
                    }
                });
            },

            //获取client的信息
            $loadClient: function () {
                var clientDetails = "";
                var clientHead = "<th>Client Name</th><th>Wechat ID</th><th></th>";
                $.ajax({
                    url: "/api/Settings/GetClientInfo",
                    data: { 'ClientName': $("#searchClient").val() },
                    dataType: "json",
                    success: function (info) {
                        $.each(info, function (j, item) {
                            //clientInfo += "<option value = " + item.ID + ">" + item.Name + "</option>";
                            clientDetails += "<tr><td>" + item.Name + "</td><td>" + item.WechatID + "</td><td><a href='javascript:DisableClient(" + item.ID + ");'>Disable</a></td></tr>";
                        });
                        $("#tb-head").html(clientHead);
                        $("#details").html(clientDetails);
                        $("#addFooter").html("<a id='addClient' href='javascript:operation.$addClient()'><i class='fa fa-plus'></i> Add Client</a>");

                    }
                });
            },

        };

        //密码重置 ---------------------------------
        function ResetPassword(OperatorID) {
            if (confirm("确定重置密码么？")) {
                $.ajax({
                    url: "/api/Settings/ResetPassWord",
                    data: { 'ID': OperatorID },
                    dataType: "json",
                    type: "post",
                    success: function (info) {
                        alert(info);
                        EmailInfo();
                    },
                    error : function(info){
                        alert('网络异常,请稍后重试');
                    }
                });
            }
        };

        //获取邮件发送信息
        function EmailInfo() {
            $.ajax({
                url: "/api/Settings/GetEmailMessage",
                dataType: "json",
                success: function (info) {
                    alert(info);
                }
            });
        };


        //获取工程师的信息--下拉复选框
        var str3 = "<option value = " + 0 + " hidden>请选择</option>";
        $.ajax({
            url: "/api/Tickets/GetOperatorInfo",
            data: { 'TicektID': 0 },
            dataType: "json",
            success: function (info) {
                $.each(info, function (j, item1) {
                    str3 += "<option value = " + item1.ID + ">" + item1.Name + "</option>";
                });
                $("#operator").append(str3);
            }
        });


        //获取公司信息--addproject中的下拉框
        function getProClient() {
            clientInfo  = "<option value = " + 0 + " hidden>请选择</option>"
            $.ajax({
                url: "/api/Settings/GetClientInfo",
                data: { 'ClientName': null },
                dataType: "json",
                success: function (info) {
                    $.each(info, function (j, item) {
                        clientInfo += "<option value = " + item.ID + ">" + item.Name + "</option>";
                    });
                    $("#proClient").html("<select id='ClientInfo' name='clientInfo'>" + clientInfo + "</select>");

                    $("#ClientInfo").blur(function () {
                        $(this).siblings(".prompt").remove();
                        if ($(this).val() == 0) {       //校验输入不为空
                            $(this).after(promptEmpty);
                        }
                    });
                }
            });
        }


        var isEmail = /^(\w)+(\.\w+)*@@(\w)+((\.\w{2,3}){1,3})$/, isMobile = /^1(3|4|5|7|8)\d{9}$/, isTel = /(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/;///(^\(\d{3,5}\)\d{6,8}(-\d{2,8})?$)|(^\d+-\d+$)|(^1(3|4|5|7|8)\d{9}$)/;
        var $data = new Array(), promptEmpty = "<span class='prompt empty'>Empty!</span>", promptError = "<span class='prompt error'>Error!</span>";

        var operation = {
            //提交时校验表单数据不为空
            $checkEmptyData: function () {
                var subFlag = 1;
                $(".empty").remove();      //去掉为空的错误输出
                $("#outputInfo input[type='text']").each(function () {
                    if (!$(this).val()) {       //输入为空
                        $(this).after(promptEmpty);
                        subFlag *= 0;
                    } else {
                        subFlag *= 1;
                    }

                });
                return subFlag;     //subFlag=1,则全不为空；subFlag=1,则全为空
            },


            //addOperator
            $addOperator: function () {
                page.$showOutput();
                var outputOperator, projectInfo = "";
                outputOperator = "<tr><td>UserName</td><td><input id='newUserName' type='text'></td></tr>"
                    + "<tr><td>Mobile</td><td><input id='newMobile' type='text'></td></tr>"
                    + "<tr><td>E-mail</td><td><input id='newEmail' type='text'></td></tr>"
                    + "<tr><td>Telephone</td><td><input id='newTelephone' type='text'></td></tr>"
                    + "<tr><td>Project</td><td id='newprojectInfo'></td></tr>";
                $("#outputInfo").html(outputOperator);
                $(".output").append("<button id='saveNewOperator' class='saveInfo'>Save</button>");
                //获取项目的信息--checkbox
                $.ajax({
                    url: "/api/Tickets/GetApplicationInfo",
                    dataType: "json",
                    success: function (data) {
                        $.each(data, function (i, item) {
                            projectInfo += "<input name='projectCheck' type='checkbox' value = " + item.ID + ">" + item.Name;
                        });
                        $("#newprojectInfo").append(projectInfo);

                    }
                });

                //及时校验
                $("#outputInfo input[type='text']").blur(function () {
                    $(this).siblings(".prompt").remove();
                    if (!$(this).val()) {       //校验输入不为空
                        $(this).after(promptEmpty);
                    } else {
                        var inputID = $(this).attr("id");
                        var $inputID = $("#" + inputID);
                        switch (inputID) {
                            case "newEmail":
                                if (!isEmail.test($inputID.val())) {                //校验邮箱格式
                                    $inputID.after(promptError);
                                }
                                break;
                            case "newMobile":
                                if (!isMobile.test($inputID.val())) {                //校验手机号码格式
                                    $inputID.after(promptError);
                                }
                                break;
                            case "newTelephone":
                                if (!isTel.test($inputID.val())) {                //校验电话号码格式
                                    $inputID.after(promptError);
                        }
                                break;
                    }
                    }
                });

                operation.$saveNewOperator();
            },

            //保存新增的Operator
            $saveNewOperator: function () {
                $(".prompt").remove();          //去掉上一次的错误提示
                
                $("#saveNewOperator").on("click", function () {
                    if (operation.$checkEmptyData()) {
                        if (!isEmail.test($("#newEmail").val())) {      //校验邮箱格式
                            $("#newEmail").after(promptError);
                        } else if (!isMobile.test($("#newMobile").val())) {         //校验手机号码格式
                            $("#newMobile").after(promptError);
                        } else if (!isTel.test($("#newTelephone").val())) {         //校验电话号码格式
                            $("#newTelephone").after(promptError);
                        }else {
                            //获取project复选框数据
                            var proVal = new Array();
                            $("input[name='projectCheck']:checked").each(function () {
                                proVal.push($(this).val());     //把选中的project数据添加到数组中
                            });

                            $.ajax({
                                url: "/api/Settings/AddOperator",
                                data: {
                                    'AllProjectID': proVal, 'UserName': $("#newUserName").val(), 'Mobile': $("#newMobile").val(), 'Email': $("#newEmail").val(), 'Telephone': $("#newTelephone").val(), 'Password': $("#newPassword").val(), 'RoleID': $("#newRoleID").val()
                                },
                                type: 'post',
                                dataType: "json",
                                success: function (data) {
                                    if (data == "success") {
                                        page.$closeOutput();
                                        page.$loadOperator();       //重新加载Operator的信息
                                    } else {
                                        //页面输出“UserName already exist”
                                        $("#newUserName").after(promptError);
                                        $("#newUserName").siblings(".prompt").html(data);
                                    }
                                }
                            });
                        }
                    }

                });

            },

            //operator页面的编辑功能
            $editOperator: function (projectID, operatorID, project) {
                page.$showOutput();
                var editOperator = "", editPro_Ope = "";

                //获取该条数据信息
                $.ajax({
                    url: "/api/Settings/GetOperatorInfo",
                    data: { 'operatorID': operatorID },
                    success: function (info) {
                        $.each(info, function (j, OperatorItem) {
                            var infoTel = OperatorItem.TelePhone != null ? OperatorItem.TelePhone : "";
                            editOperator += "<tr><td>UserName</td><td><span id='infoUserName'>" + OperatorItem.UserName
                                + "</span></td></tr><tr><td>Mobile</td><td><input id='infoMobile' value =" + OperatorItem.Mobile
                                + "></td></tr><tr><td>E-mail</td><td><input id='infoEmail' value =" + OperatorItem.Email
                                + "></td></tr><tr><td>Telephone</td><td><input id='infoTel' value =" + OperatorItem.TelePhone
                                + "></td></tr><tr><td>Project</td><td id='projectCheck'></td></tr>";
                        });
                        $("#outputInfo").html(editOperator);

                        //获取该operator对应的project，选中
                        $.ajax({
                            url: "/api/Settings/ApplicationInfoByOperator",
                            data: { 'MemberID': operatorID },
                            dataType: "json",
                            type: "post",
                            success: function (info) {
                                $.each(info, function (j, item) {
                                    editPro_Ope += "<input name='projectCheck'type='checkbox' checked value='" + item.ProjectID + "'/>" + item.Project;
                                });
                                operation.$editOperatorPro(projectID, operatorID, editPro_Ope);
                            }
                        });

                    }
                });
            },
            //获取该operator的项目信息--checkbox
            $editOperatorPro: function (projectID, operatorID, editPro_Ope) {
                $.ajax({
                    url: "/api/Tickets/GetApplicationInfo",
                    dataType: "json",
                    success: function (data) {
                        $.each(data, function (i, item) {
                            if (editPro_Ope.indexOf(item.ID) == -1)   //判断该project是否已存在于operator中
                                editPro_Ope += "<input name='projectCheck' type='checkbox' value = " + item.ID + ">" + item.Name;
                        });
                        $("#projectCheck").append(editPro_Ope);
                        $(".output").append("<button id='saveEditOperator' class='saveInfo'>Save</button>");

                        //及时校验
                        $("#outputInfo input").blur(function () {
                            $(this).siblings(".prompt").remove();
                            if (!$(this).val()) {       //校验输入不为空
                                $(this).after(promptEmpty);
                            } else {
                                var inputID = $(this).attr("id");
                                var $inputID = $("#" + inputID);
                                switch (inputID) {
                                    case "infoEmail":
                                        if (!isEmail.test($inputID.val())) {                //校验邮箱格式
                                            $inputID.after(promptError);
                                        }
                                        break;
                                    case "infoMobile":
                                        if (!isMobile.test($inputID.val())) {                //校验手机号码格式
                                            $inputID.after(promptError);
                                        }
                                        break;
                                    case "infoTel":
                                        if (!isTel.test($inputID.val())) {                //校验电话号码格式
                                            $inputID.after(promptError);
                                        }
                                        break;
                                }
                            }
                        });

                        operation.$saveOperator(projectID, operatorID);       //调用Edit-Save方法
                    }
                });
            },

            //operator页面-Edit保存功能
            $saveOperator: function (projectID, operatorID) {
                $("#saveEditOperator").on("click", function () {
                    if (operation.$checkEmptyData()) {          //判断表单是否为空
                        var editFlag = 1;
                        if (!isEmail.test($("#infoEmail").val())) {      //校验邮箱格式
                            $("#infoEmail").after(promptError);
                            editFlag *= 0;
                        }
                        if (!isMobile.test($("#infoMobile").val())) {         //校验手机号码格式
                            console.log($(this));
                            $("#infoMobile").after(promptError);
                            editFlag *= 0;
                        }
                        if (!isTel.test($("#infoTel").val())) {         //校验电话号码格式
                            $("#infoTel").after(promptError);
                            editFlag *= 0;
                        }
                        if(editFlag){       //判断填写格式是否全部正确
                    //获取project复选框数据
                    var proVal = new Array();
                    $("input[name='projectCheck']:checked").each(function () {
                        proVal.push($(this).val());     //把选中的数据添加到数组中
                    });

                    $.ajax({
                        url: "/api/Settings/ModifyOperatorInfoAndMap",
                                data: { 'AllProjectID': proVal, 'OperatorID': operatorID, 'Mobile': $("#infoMobile").val(), 'Email': $("#infoEmail").val(), 'TelePhone': $("#infoTel").val() },
                        type: 'post',
                        dataType: "json",
                        success: function (data) {
                            if (data == "success") {
                                page.$closeOutput();
                                page.$loadOperator();       //重新加载Operator的信息
                            } else {
                                alert(data);
                            }
                        }
                    });
                        }
                    }
                });
            },
//-----------------------------------------------------------------------------------------------------------
            //addProject
            $addProject: function () {
                page.$showOutput();
                getProClient();     //获取client信息
                var outputProject = "<tr><td>Project Name</td><td><input id='ProjectNameInfo' type='text'></td></tr>" +
                    "<tr><td>Client</td><td id='proClient'></td></tr>" + "<tr><td>WeChat Active</td><td><select id='wechatActive' name='wechatActive'><option name='wechatActive' value='true'>True</option><option name='wechatActive' value='false'>False</option></select></td></tr><tr><td>On Call</td><td><input id='oncall' type='text'></td></tr>";
                $("#outputInfo").html(outputProject);
                $(".output").append("<button id='saveNewProject' class='saveInfo'>Save</button>");

                //及时校验
                $("#ProjectNameInfo").blur(function () {
                    $(this).siblings(".prompt").remove();
                    if (!$(this).val()) {       //校验输入不为空
                        $(this).after(promptEmpty);
                    }
                });
                $("#oncall").blur(function () {
                    $(this).siblings(".prompt").remove();
                    if (!$(this).val()) {       //校验输入不为空
                        $(this).after(promptEmpty);
                    } else if (!isTel.test($(this).val())) {                //校验电话号码格式
                        $(this).after(promptError);
                    }
                });

                operation.$saveNewProject();
                
            },

            $saveNewProject:function(){
                //保存数据
                $("#saveNewProject").on("click",function () {
                    var editFlag = 1, projectName = $("#ProjectNameInfo").val(), clientInfo = $("#ClientInfo").val(), IsWeChatActive = $("#wechatActive").val(), OnCall = $("#oncall").val();
                    if (!projectName) {          //判断projectName是否为空
                        $("#ProjectNameInfo").after(promptEmpty);
                        editFlag *= 0;
                    }
                    if (clientInfo == 0) {          //判断client是否为空
                        $("#ClientInfo").after(promptEmpty);
                        editFlag *= 0;
                    }

                    if (OnCall == 0) {       //校验输入不为空
                        $("#oncall").after(promptEmpty);
                        editFlag *= 0;
                    } else if (!isTel.test(OnCall)) {                //校验电话号码格式
                        $("#oncall").after(promptError);
                        editFlag *= 0;
                    }

                    if (editFlag) {
                        $.ajax({
                            url: "/api/Settings/AddProject",
                            data: { 'ProjectName': projectName, 'ClientID': clientInfo, 'IsWeChatActive': IsWeChatActive,'OnCall':OnCall},
                            type: "POST",
                            dataType: "json",
                            success: function (info) {
                                if (info == "success") {
                                    page.$closeOutput();
                                    page.$loadProject();        //重新加载project的信息
                                } else {
                                    //页面输出“ProjectName already exist”
                                    $("#ProjectNameInfo").after(promptError);
                                    $("#ProjectNameInfo").siblings(".prompt").html(info);
                                }
                            }
                        });
                    }

                });
            },

            //project页面的编辑功能
            $editProject: function (ProjectID, IsActive) {
                page.$showOutput();
                var wechatActive, editProOutput = "";
                wechatActive = "<select id='eWechatActive' name='eWechatActive'><option name='eWechatActive' val='" + IsActive + "'>" + IsActive + "</option><option name='eWechatActive' val='" + !IsActive + "'>" + !IsActive + "</option></select>";
                editProOutput += "<tr><td><label for=''>Project Name</label></td><td><span>" + $("#ProjectName" + ProjectID).html() + "</span></td></tr>"
                    + "<tr><td><label for=''>Client</label></td><td><span>" + $("#ClientName" + ProjectID).html() + "</span></td></tr>"
                    + "<tr><td><label for=''>Wechat Active</label></td><td>" + wechatActive + "</td></tr>"
                    + "<tr><td><label for=''>Issue Type</label></td><td><span id='IssueTypeEdit" + ProjectID + "'></span></td></tr>"
                    + "<tr id='addIssueTypeTr'><td colspan='2'><a style='margin: 0;' href='javascript:operation.$addIssueType_Pro();'><i class='fa fa-plus'></i> Issue Type</a></td></tr>";
                $("#outputInfo").html(editProOutput);
                $(".output").append("<button id='saveEditProject' class='saveInfo'>Save</button>");

                /*获取该project中存在的Issue Type*/
                var IssueType = "";
                $.ajax({
                    url: "/api/Settings/GetIssueTypeInfos",
                    data: { 'ProjectID': ProjectID },
                    dataType: "json",
                    success: function (info) {
                        $.each(info, function (j, item) {

                            IssueType += "<div class='issueTypeTag' id='" + item.ID + "'>" + $.trim(item.IssueTypeDescription) + "<i class='fa fa-remove removeIssueType'></i></div>";
                        });
                        $("#IssueTypeEdit" + ProjectID).append(IssueType);
                        operation.$removeIssueType();
                        operation.$saveEditProject(ProjectID);       //调用Edit-Sava方法
                        //operation.$editProjectIssueType(ProjectID, IssueTypeVal);       //加载其余可供添加的Issue Type
                    }
                });


            },

            //删除project中的issue type
            $removeIssueType: function () {
                $(".removeIssueType").click(function () {
                    $(this).parent().remove();
                });
            },

            //新增该项目的Issue Type
            $addIssueType_Pro: function () {
                var inputNewIssueType = "<td style='padding-right: 0;'><label for='newIssueType'>New Issue Type</label></td><td><input type='text' id='newIssueType'><select name='toType' id='toType' style='width: 120px;margin-left: 40px;'><option name='toType' value='incident'>Incident</option><option name='toType' value='request'>Request</option></select></td>";
                $("#addIssueTypeTr").html(inputNewIssueType);
            },

            //加载其余可供添加的Issue Type
            //$editProjectIssueType: function (ProjectID, IssueTypeVal) {
            //    var IssueTypeDescription;
            //    $.ajax({
            //        url: "/api/Settings/GetIssueTypeInfos",
            //        data: { 'ProjectID': 0 },
            //        dataType: "json",
            //        success: function (info) {
            //            $.each(info, function (j, item) {
            //                IssueTypeDescription = $.trim(item.IssueTypeDescription);
            //                if (IssueTypeVal.indexOf(IssueTypeDescription) == -1)   //判断是否已存在于该project中
            //                    IssueTypeVal += "<input type='checkbox' name='IssueTypeCheck' value=" + item.ID + ">" + IssueTypeDescription;
            //            });
            //            $("#IssueTypeEdit" + ProjectID).append(IssueTypeVal);
            //            operation.$saveEditProject(ProjectID);       //调用Edit-Sava方法
            //        }
            //    });
            //},

            

            ///////////////////////////待添加新增Issue Type方法///////////////////////////////
            //project页面-Edit保存功能
            $saveEditProject: function (ProjectID) {
                $("#saveEditProject").on("click", function () {
                    //获取IssueType复选框数据
                    var IssueTypeVal = new Array();
                    $(".issueTypeTag").each(function () {
                        IssueTypeVal.push($(this).attr("id"));     //把选中的数据添加到数组中
                    });
                    //alert(IssueTypeVal);
                    $.ajax({
                        url: "/api/Settings/ModifyIssueTypeAndMap",
                        data: { 'IssueTypeID': IssueTypeVal, 'ApplicationID': ProjectID, 'NewIssueType': $("#newIssueType").val(), 'Type': $("#toType").val(), 'IsWeChatActive': $("#eWechatActive").val() },
                        type: 'post',
                        dataType: "json",
                        success: function (data) {
                            if (data == "success") {
                                page.$closeOutput();
                                page.$loadProject();        //重新加载project的信息
                            } else {
                                alert(data);
                            }
                        }
                    });
                });
            },


            //addClient
            //change by kingkang
            //changed Info:outputClient
            $addClient: function () {
                page.$showOutput();
                var outputClient =
                    "<tr><td>Client</td><td><input id='ClientName' type='text'></td></tr>"
                    + "<tr><td>CropID</td><td><input id='CropID' type='text' /></td></tr>"
                    + "<tr><td>Secret</td><td><input id='Secret' type='text'/></td></tr>"
                    + "<tr><td>AppID</td><td><input id='AppID' type='text'/></td></tr>"
                    + "<tr><td>GuuID</td><td id='Guuid'>" + guid() + "</td></tr>"
                    + "<tr><td>Logo</td><td><input id='Logo' type='file'/></td></tr>";
                $("#outputInfo").html(outputClient);
                $(".output").append("<button id='saveNewClient' class='saveInfo'>Save</button>");

                //及时校验
                $("#outputInfo input[type='text']").each(function () {
                    $(this).blur(function () {
                        $(this).siblings(".prompt").remove();
                        if (!$(this).val()) {       //输入为空
                            $(this).after(promptEmpty);
                        }
                    })
                });

                //产生唯一的guuid
                function S4() {
                    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
                }
                //只产生了11位
                function guid() {
                    return (S4() + S4() + S4()).substring(1);
                }
                    //保存数据
                $("#saveNewClient").on("click", function () {
                    var clientName = $("#ClientName").val(), CropID = $("#CropID").val(), Secret = $("#Secret").val(), AppID = $("#AppID").val(), Guuid = $("#Guuid").html();
                    if (operation.$checkEmptyData()) {      //校验表单是否为空
                        var formData = new FormData();
                        formData.append("file", document.getElementById("Logo").files[0]);
                        formData.append("clientName", clientName);
                        formData.append("CropID", CropID);
                        formData.append("Secret", Secret);
                        formData.append("AppID", AppID);
                        formData.append("Guuid", Guuid);
                        $.ajax({
                            url: "/api/Settings/AddClient",
                            data: formData,
                            type: "POST",
                            contentType: false,
                            processData: false,
                            success: function (info) {
                                if (info == "success") {
                                    page.$closeOutput();
                                    page.$loadClient();     //重新加载client的信息
                                } else {
                                    alert(info);
                                }
                            }
                        });
                    }
                });
            },

        }

        function DisableOperator(operatorID) {
            $.ajax({
                url: "/api/Settings/ModifyOperatorStatus",
                data: { 'ID': operatorID, 'Status': false },
                dataType: "json",
                type: "post",
                success: function (info) {
                    if (info == "success") {
                        page.$loadOperator();       //重新加载Operator的信息
                    } else {
                        alert(info);
                    }
                }
            });
        };

        function DisableProject(projectID) {
            $.ajax({
                url: "/api/Settings/ModifyApplicationStatus",
                data: { 'ID': projectID, 'Status': false },
                dataType: "json",
                type:"post",
                success: function (info) {
                    if (info == "success") {
                        page.$loadProject();        //重新加载project的信息
                    } else {
                        alert(info);
                    }
                }
            });
        };

        function DisableClient(ClientID) {
            $.ajax({
                url: "/api/Settings/ModifyClientStatus",
                data: { 'ID': ClientID, 'Status': false },
                dataType: "json",
                type: "post",
                success: function (info) {
                    if (info == "success") {
                        page.$loadClient();     //重新加载client的信息
                    } else {
                        alert(info);
                    }
                }
            });
        };

    </script>

    </body>
</html>

